//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.spring.schedule_system.dengxiaojiao.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.alibaba.fastjson.parser.Feature;
import java.util.ArrayList;
import java.util.List;
import org.springframework.util.CollectionUtils;

public class LyListResultUtil {
    public LyListResultUtil() {
    }

    public static <T> ListResult<T> success(List<T> t) {
        return success(0L, 0L, 0L, t, (String)null);
    }

    public static <T> ListResult<T> success(List<T> t, String msg) {
        return success(0L, 0L, 0L, t, msg);
    }

    public static <T> ListResult<T> success(long pageindex, long pages, long records, List<T> t) {
        return success(pageindex, pages, records, t, (String)null);
    }


    public static <T> ListResult<T> success(long pageindex, long pages, long records, List<T> t, String msg) {
        ListResult<T> result = new ListResult();
        result.setPageindex((int)pageindex);
        result.setPages((int)pages);
        result.setRecords((int)records);
        if (!CollectionUtils.isEmpty(t)) {
            result.setRows(t);
        } else {
            result.setRows(new ArrayList());
        }

        result.setMsg(msg);
        return result;
    }

    public static <T> ListResult<T> fail(String errorCode, String msg) {
        ListResult<T> result = new ListResult();
        result.setResult(errorCode);
        result.setMsg(msg);
        result.setRows(new ArrayList());
        return result;
    }

    public static <R, T> ListResult<R> convertTo(ListResult<T> t, Class<R> clz) {
        ListResult<R> result = new ListResult();
        if (null != t) {
            if (!CollectionUtils.isEmpty(t.getRows())) {
                if (t.getRows().get(0).getClass() == clz) {
                    LyBeanUtils.copyProperties(t, result);
                } else {
                    result.setMsg(t.getMsg());
                    result.setPageindex(t.getPageindex());
                    result.setPages(t.getPages());
                    result.setRecords(t.getRecords());
                    result.setResult(t.getResult());
                    result.setRows(LyListUtil.convertTo(t.getRows(), clz));
                }
            } else {
                result.setMsg(t.getMsg());
                result.setPageindex(t.getPageindex());
                result.setPages(t.getPages());
                result.setRecords(t.getRecords());
                result.setResult(t.getResult());
                result.setRows(new ArrayList());
            }
        }

        return result;
    }


    public static <R, T> ListResult<R> convertToByJSON(ListResult<T> t, Class<R> clz) {
        return (ListResult)JSON.parseObject(JSON.toJSONString(t), new TypeReference<ListResult<R>>() {
        }, new Feature[0]);
    }


    public static int getPages(int pageSize, int total) {
        if (pageSize == 0) {
            return 0;
        } else {
            int pages = total / pageSize;
            if (total % pageSize != 0) {
                ++pages;
            }

            return pages;
        }
    }
}
